package com.hudongwx.shansun.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.hudongwx.shansun.activity.DisplayActivity;
import com.hudongwx.shansun.activity.HomeSelectActivity;
import com.hudongwx.shansun.activity.LoginActivity;
import com.hudongwx.shansun.R;
import com.hudongwx.shansun.activity.LimitHomeActivity;
import com.hudongwx.shansun.activity.TitleActivity;
import com.hudongwx.shansun.utlis.API;
import com.hudongwx.shansun.utlis.MyJSONobject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class Fragment1 extends Fragment {

    private ListView listView;
    private MyHomeAdapter myHomeAdapter;
    private String TAG = "Fragment1";
    private JSONObject my;
    private ImageView iv1, iv, iv2, iv3, iv4, iv5;

    private ImageView iv6, iv7;
    private List<ImageView> imgs;
    private Map<String, String> maptitle;
    private View view;
    private String linkId, title, viewType;
    private FastDownClickListener fastDownClickListener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //  View view = x.view().inject(new Fragment1(), inflater, container);
        if (null == view) {
            view = inflater.inflate(R.layout.fragment_1, container, false);
            listView = (ListView) view.findViewById(R.id.fragment_1_listview);

            iv6 = (ImageView) view.findViewById(R.id.fragment_1_iv);
            iv7 = (ImageView) view.findViewById(R.id.fragment_1_iv_2);
            iv7.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                }
            });
            iv6.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(getActivity(), HomeSelectActivity.class));
                }
            });
            RequestParams params = new RequestParams(API.fragment_home);
            x.http().get(params, new Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    try {
                        my = new MyJSONobject(result);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    fastDownClickListener=new FastDownClickListener();
                    myHomeAdapter = new MyHomeAdapter(my);
                    listView.setAdapter(myHomeAdapter);
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {

                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }
            });
        }

        return view;
    }

    class MyHomeAdapter extends BaseAdapter {
        private JSONObject data;


        public MyHomeAdapter(JSONObject data) {
            this.data = data;
        }

        @Override
        public int getCount() {
            try {
                JSONArray items = data.getJSONArray("data");
                return items.length() + 1;
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return 0;
        }

        private View dataLoadFirst(JSONObject json) {
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.home_item_first, null);
            LinearLayout ll = (LinearLayout) view.findViewById(R.id.home_item_first_ll);
            imgs = new ArrayList<>();

            iv1 = (ImageView) view.findViewById(R.id.home_item_first_iv_1);
            iv2 = (ImageView) view.findViewById(R.id.home_item_first_iv_2);
            iv3 = (ImageView) view.findViewById(R.id.home_item_first_iv_3);
            iv4 = (ImageView) view.findViewById(R.id.home_item_first_iv_4);
            iv5 = (ImageView) view.findViewById(R.id.home_item_first_iv_5);

            imgs.add(iv1);
            imgs.add(iv2);
            imgs.add(iv3);
            imgs.add(iv4);


            ll.setOrientation(LinearLayout.HORIZONTAL);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.weight = 1;
            try {
                JSONArray arry = data.getJSONArray("templates").getJSONObject(0).getJSONArray("item");
                JSONArray arry1 = data.getJSONArray("templates").getJSONObject(1).getJSONArray("item");
                JSONArray arry2 = data.getJSONArray("templates").getJSONObject(2).getJSONArray("item");
                for (int i = 0; i < arry1.length(); i++) {
                    JSONObject jso = arry1.getJSONObject(i);
                    String url = jso.getString("imgUrl");
                    String link = jso.getString("link");

                    imgs.get(i).setTag(link);
                    imgs.get(i).setOnClickListener(new HomeTitleClick());
                    x.image().bind(imgs.get(i), url);
                }


                JSONObject jso5 = arry2.getJSONObject(0);
                String url_iv5 = jso5.getString("imgUrl");
                x.image().bind(iv5, url_iv5);

                int len = arry.length();
                titleOnClick titleOnClick = new titleOnClick();
                maptitle = new HashMap<>();
                for (int i = 0; i < len; i++) {
                    JSONObject item = arry.getJSONObject(i);
                    String url = item.getString("imgUrl");
                    iv = new ImageView(getActivity());
                    iv.setLayoutParams(params);
                    String name = item.getString("name");

                    iv.setTag(item.getString("link")+"|"+item.getString("name"));
                    iv.setOnClickListener(titleOnClick);
                    x.image().bind(iv, url);
                    ll.addView(iv);

                }

            } catch (JSONException e) {
                e.printStackTrace();
            }


            return view;

        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (position == 0) {
                if (convertView != null && convertView instanceof LinearLayout) {
                    return convertView;
                } else {
                    return dataLoadFirst(data);
                }


            } else {
                try {
                    ImageView imageView = null;
                    if (convertView != null && convertView instanceof ImageView) {
                        imageView = (ImageView) convertView;
                    } else {
                        convertView = getActivity().getLayoutInflater().inflate(R.layout.home_content_item, null);
                        imageView = (ImageView) convertView.findViewById(R.id.home_item_content_iv);
                    }

                    JSONArray items = data.getJSONArray("data");
                    JSONObject jsonObject = items.getJSONObject(position - 1);
                    x.image().bind(imageView, jsonObject.getString("imgUrl"));
                    imageView.setTag(jsonObject.getString("linkId")+"|"+jsonObject.getString("title")+"|"+jsonObject.getString("type"));
                    imageView.setOnClickListener(fastDownClickListener);
                    linkId = jsonObject.getString("linkId");
                    title = jsonObject.getString("title");
                    viewType = jsonObject.getString("type");
                    return convertView;
                } catch (JSONException e) {
                    e.printStackTrace();

                }


                return new TextView(getActivity());
            }
        }


    }

    class HomeTitleClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            Intent intent2 = new Intent(getActivity(), LimitHomeActivity.class);
             intent2.putExtra("url",v.getTag()+"");
            startActivity(intent2);
        }
    }


    class FastDownClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), DisplayActivity.class);
            intent.putExtra("url", v.getTag() + "");
            startActivity(intent);
            Log.i(TAG, "onClick: "+v.getTag()+"");
        }
    }
    class titleOnClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), TitleActivity.class);
            intent.putExtra("url",v.getTag()+"");
            startActivity(intent);


        }
    }


}

